/**
 * 根据用户菜单权限设置路由
 * @param menuData
 */
/* -------------------------------------分割线-------------------------------------------- */
import layout from '../layout/index.vue'
import formTemplate1 from '../views/FormTemplate/template1.vue'
import formTemplate2 from '../views/FormTemplate/template2.vue'
import introduce from '../views/TableTemplate/introduce.vue'
import tableTemplate1 from '../views/TableTemplate/template1.vue'
import tableTemplate2 from '../views/TableTemplate/template2.vue'
import cardTemplate1 from '../views/CardTemplate/template1.vue'
import cardTemplate2 from '../views/CardTemplate/template2.vue'
import cardTemplate3 from '../views/CardTemplate/template3.vue'
import cardTemplate4 from '../views/CardTemplate/template4.vue'
import document from '../views/document/index.vue'
import MyFolder from '../views/document/pages/MyFolder.vue'
import MyFolderTest from '../views/document/pages/MyFolderTest.vue'
import MyKnowledge from '../views/document/pages/MyKnowledge.vue'
import MyBin from '../views/document/pages/MyBin.vue'
import upload from '../views/upload/index.vue'
import process from '../views/process/index.vue'
import system from '../views/system/index.vue'
import trial from '../views/trial/index.vue'
import fileEdit from '../views/fileEdit/index.vue'
import home from '../views/home/index.vue'


const componentObj = {
  layout,
  document,
  upload,
  process,
  system,
  formTemplate1,
  formTemplate2,
  introduce1: introduce,
  introduce2: trial,
  tableTemplate1,
  tableTemplate2,
  cardTemplate1,
  cardTemplate2,
  cardTemplate3,
  cardTemplate4,
  trial,
  fileEdit,
  home,
  MyFolder,
  MyFolderTest,
  MyKnowledge,
  MyBin,
}
const iconList = {
  document: 'menu_document', //menu_开头为img
  upload: 'menu_upload', //menu_开头为img
  process: 'menu_process', //menu_开头为img
  system: 'menu_system', //menu_开头为img
  formTemplate: 'Tickets',
  tableTemplate: 'CopyDocument',
  cardTemplate: 'Files',
  trial: 'menu_document',
}
/**
 * 动态添加用户路由权限
 * @param menuData json格式的字符串
 * @returns {[]}
 */
export function getAuthRouters2(menuData) {
  const routerList = []
  let obj, children
  menuData && menuData.map(item => {
    obj = {
      path: item.path,
      component: layout,
      redirect: item.isDirectLink ? `${item.path}` : 'noRedirect',
      name: item.name,
      isDirectLink: item.isDirectLink || false,
      meta: {
        breadcrumbName: item.meta.title,
        icon: iconList[item.name],
        isDisable: item.meta?.isDisable || false
      }
    }
    // 设置子菜单
    children = []
    item['children'].map(it => {
      children.push({
        path: it.path,
        component: componentObj[it.name] || it.component,
        name: it.name, // 如果该组件需要缓存,这里的name属性请与组件的name保持一致
        isDirectLink: it.isDirectLink || false,
        meta: {
          breadcrumbName: it.meta.title,
          cached: true,
          keepAlive: it.meta.keepAlive || false // 是否需要缓存
        }
      })
    })
    obj.children = children
    routerList.push(obj)
  })
  return routerList
}

export function getAuthRouters(menuData) {
  // 递归转换函数
  function transform(item) {
    let route = {
      path: item.path,
      name: item.name,
      redirect: item.redirect || '',
      isDirectLink: item.isDirectLink || false,
      component: componentObj[item.name] || item.component,
      meta: {
        breadcrumbName: item.meta.breadcrumbName || item.meta.title,
        cached: item.meta.cached || false,
        keepAlive: item.meta.keepAlive || false,
        isDisable: item.meta.isDisable || false,
        title: item.meta.title || item.meta.breadcrumbName,
        // 如果有 icon 处理逻辑
        // icon: item.meta.icon || '',
        icon: iconList[item.name],
      }
    };

    // 如果该项为直接链接
    if (item.isDirectLink) {
      // 如果存在子级，则需要添加默认子路由，用空字符串 path 保证 /document 直接加载组件
      if (item.children && item.children.length > 0) {
        console.log(item);
        // route.redirect = item.path;
        route.redirect = item.redirect || '';
        route.component = componentObj[item.name] || item.component
        // 默认子路由，用于显示本组件
        // let defaultChild = {
        //   path: '',
        //   name: item.name + '-index',
        //   isDirectLink: item.isDirectLink || false,
        //   component: componentObj[item.name] || item.component,
        //   redirect: item.redirect || '',
        //   meta: {
        //     ...route.meta
        //   }
        // };
        // 递归处理其他子路由（如果有更多层级）
        let childRoutes = item.children.map(child => transform(child));
        // route.children = [defaultChild, ...childRoutes];
        route.children = [...childRoutes];
      } else {
        // 没有子级，直接赋值组件
        route.component = componentObj[item.name] || item.component;
      }
    } else {
      // 非直接链接项，可能是需要展开子菜单的项
      // 若该项有组件也可以赋值，但通常用于有子路由的父级
      route.component = componentObj[item.name] || item.component;
      if (item.children && item.children.length > 0) {
        route.children = item.children.map(child => transform(child));
      }
    }
    return route;
  }

  // 对 menuData 数组中每个项进行转换
  const transformedRoutes = menuData.map(item => transform(item));
  // 包裹在 layout 下
  console.log(transformedRoutes, '2310381');

  return [
    {
      path: '/',
      component: layout,
      children: transformedRoutes,
      redirect: '/home',
    }
  ];
}


